package com.wes.dsptest.info;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Log;


public class InfoPresenter {
    private Context context;
    private Object object = new Object();
    private static boolean updatePause = false;
    private Runnable updatetimeRunnable;
    private Handler updateHandler;
    private static final String TAG = "InfoPresenter";

    public InfoPresenter(Context context){
        this.context = context;
    }

    public String getDeviceVersion(){
        String name = Build.DEVICE;
        return name;
    }

    public String getAppVersion(){
        String versionName = null;
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    public String getDeviceMem(){
        String memInfo = null;
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(memoryInfo);
        long totalMemory = memoryInfo.totalMem;
        long availableMemory = memoryInfo.availMem;
        float total = ((float)totalMemory)/1024/1024/1024;
        float available = ((float)availableMemory)/1024/1024/1024;
        memInfo = String.format("%.2f",available) + "G/" + String.format("%.2f",total) + "G";
        return memInfo;
    }



    /**
     * 将毫秒数转换为可读的格式
     *
     * @param millis 毫秒数
     * @return 可读的时间格式，如"1小时23分45秒"
     */
    private static String formatDuration(long millis) {
        long seconds = millis / 1000;
        long minutes = seconds / 60;
        long hours = minutes / 60;
        long days = hours / 24;

        StringBuilder sb = new StringBuilder();
        if (days > 0) {
            sb.append(days).append("天");
        }
        if (hours > 0) {
            sb.append(hours % 24).append("小时");
        }
        if (minutes > 0) {
            sb.append(minutes % 60).append("分");
        }
        if (seconds > 0) {
            sb.append(seconds % 60).append("秒");
        }
        return sb.toString();
    }

    public void startRunTime(InfoView runTimeView){

        updateHandler = new Handler(Looper.getMainLooper());
        updatetimeRunnable = new Runnable() {
            @Override
            public void run() {
                long update = SystemClock.uptimeMillis();
                String duration = formatDuration(update);
                runTimeView.getContentView().setText(duration);
                Log.i(TAG, "info time:" + duration);
                // 循环执行
                synchronized (object) {
                    if(!updatePause){
                        updateHandler.postDelayed(updatetimeRunnable, 1000);
                    }
                }
            }
        };
        updateHandler.post(updatetimeRunnable);
    }

    public void pause(){
        updatePause = true;

    }

    public void resume(){

        synchronized (object){
            if(updatePause)
            {
                updateHandler.post(updatetimeRunnable);
                updatePause = false;
            }
        }
    }

    public void close(){
        if(updateHandler != null)
            updateHandler.removeCallbacks(updatetimeRunnable);
    }

}
